package com.yfeng.goodjuhome.model;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yfeng.common.dao.IDao;
import com.yfeng.common.dao.Param;
@Component
public class Group {
	
	private String id;
	private String pid;
	private String name;
	
	@Autowired
	public IDao baseDao;
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public boolean isExist(String name) throws Exception {

		int ret = baseDao.getRowCount("from groups where name=:name", new Param("name", name));
		if (ret > 0)
			return true;
		else
			return false;

	}
	public List getGroups()throws Exception{
		return baseDao.query("select * from groups");
	}
	public List getGroupsByUserId(String userid)throws Exception{
		return baseDao.query("select * from groups where uid='"+userid+"'");
	}
	public  Group getGroupById(String id) throws Exception {
		Group group=new Group();
		List userList = baseDao.query("select t1.* from groups t1 where t1.id=:id",new Param("id", id));
		Map groupRow = (Map) userList.get(0);
		group.setId(id);
		group.setName(groupRow.get("name").toString());
		Object pid=groupRow.get("pid");
		if(pid!=null)
			group.setPid(pid.toString());
		return group;
	}
}
